
<template>
  <div class="login-container">
    <div class="login-box">
      <el-tabs v-model="activeRole" class="role-tabs">
        <el-tab-pane label="学生" name="student"></el-tab-pane>
        <el-tab-pane label="老师" name="teacher"></el-tab-pane>
        <el-tab-pane label="管理员" name="admin"></el-tab-pane>
      </el-tabs>
      <el-form class="login-form" :model="form" :rules="rules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input :prefix-icon="User" v-model="form.username" 
          v-if="activeRole == 'student'" placeholder="请输入学号" size="large"/>
          <el-input :prefix-icon="User" v-model="form.username" 
          v-if="activeRole == 'teacher'" placeholder="请输入工号" size="large"/>
          <el-input :prefix-icon="User" v-model="form.username" 
          v-if="activeRole == 'admin'" placeholder="请输入管理员账号" size="large"/>
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon="Lock" v-model="form.password" type="password" placeholder="请输入密码" show-password size="large"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" size="large" style="width: 100%">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import {Lock,User} from '@element-plus/icons-vue'
import {useRouter} from 'vue-router'
import {useUserStore} from '@/stores/userStores'
//  ref  定义响应式数据（html 和 js 的数据是用的一个  html的数据变了 js的数据也会变）  
const activeRole = ref('student')

const userStrore = useUserStore()

const form = ref({
  username: '',
  password: '',
  role : ''
})
// 定义表单校验规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ]
}
// 定义表单引用
const loginFormRef = ref(null)

const router = useRouter()
const handleLogin = () => {


loginFormRef.value?.validate((valid : boolean) => {

    if (valid) {
      // 表单校验通过，执行登录逻辑

      // console.log(activeRole.value);
      form.value.role = activeRole.value;

      console.log('登录信息:', form.value)

      // 登录逻辑，例如调用API进行身份验证 


      // 登录成功后，跳转到主页或其他页面
      userStrore.setUserInfo(form.value)
      // 例如，使用 Vue Router 进行页面跳转
     


      router.push('/')

    }
    else {
      // 表单校验失败，显示错误信息
      console.log('登录失败')
    }
  })

}
</script>

<style scoped>

.login-container {
  height: 100vh;
  background-image: url('@/assets/school-bg.png');
  background-size: cover;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-box {
  width: 400px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.role-tabs {
  margin-bottom: 20px;
}

.login-form {
  margin-top: 20px;
}

.login-form .el-form-item {
  margin-bottom: 24px;
}
</style>